<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="抽象类，接口">
<meta property="og:type" content="article">
<meta property="og:title" content="Java日拱一卒-day9">
<meta property="og:url" content="http://yoursite.com/2020/04/22/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day9/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="抽象类，接口">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-22T02:11:40.000Z">
<meta property="article:modified_time" content="2020-05-17T18:16:07.928Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/04/22/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day9/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java日拱一卒-day9 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/22/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day9/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java日拱一卒-day9
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-22 10:11:40" itemprop="dateCreated datePublished" datetime="2020-04-22T10:11:40+08:00">2020-04-22</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-18 02:16:07" itemprop="dateModified" datetime="2020-05-18T02:16:07+08:00">2020-05-18</time>
              </span>

          
            <div class="post-description">抽象类，接口</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="抽象方法"><a href="#抽象方法" class="headerlink" title="抽象方法"></a>抽象方法</h2><p>抽象方法和抽象类必须使用<code>abstract</code>修饰符来定义，由抽象方法的类只能定义成抽象类，抽象类里可以没有抽象方法。</p>
<ul>
<li>抽象类必须使用<code>abstract</code>修饰符来修饰，抽象方法必须使用<code>abstract</code>修饰，抽象方法不能有方法体。</li>
<li>抽象类不能被实例化，无法使用<code>new</code>关键字来调用抽象类的构造去来创建抽象类的实例。即使抽象类里不包含抽象方法，这个抽象类也不囊创建实例。</li>
<li>抽象类可以包含成员变量、方法（普通方法或抽象方法）、构造方法、初始化块、内部类（接口，枚举）5种成分。抽象类的构造方法主要是用于被其子类调用。</li>
<li>含有抽象方法类只能被定义成抽象类</li>
</ul>
<p>定义一个<code>Shape</code>抽象类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Shape</span> </span>&#123;</span><br><span class="line">    &#123;</span><br><span class="line">        System.out.println(<span class="string">"执行Shape的初始化块"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">private</span> String color;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 定义一个计算周长的抽象方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">double</span> <span class="title">calPerimeter</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义一个返回形状的抽象方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> String <span class="title">getType</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//定义Shape的构造方法，用于被子类调用</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Shape</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Shape</span><span class="params">(String color)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"执行Shape的构造方法"</span>);</span><br><span class="line">        <span class="keyword">this</span>.color = color;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getColor</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> color;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setColor</span><span class="params">(String color)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.color = color;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的<code>Shape</code>类包含两个抽象方法：<code>calPerimeter()</code>和<code>getType()</code>，所以这个类是个抽象类。抽象类不能用于创建实例，只能当作父类被其他子类继承。下面定义的三角形类是一个普通类，因此必须实现<code>Shape</code>类里所有的抽象方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Triangle</span> <span class="keyword">extends</span> <span class="title">Shape</span> </span>&#123;</span><br><span class="line"><span class="comment">//定义三个边</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> a;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> b;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> c;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span>  <span class="title">Triangle</span><span class="params">(String color ,<span class="keyword">double</span> a,<span class="keyword">double</span> b,<span class="keyword">double</span> c)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(color);</span><br><span class="line">        <span class="keyword">this</span>.setSides(a,b,c)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSides</span><span class="params">(<span class="keyword">double</span> a, <span class="keyword">double</span> b, <span class="keyword">double</span> c)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (a &gt;= b + c || b &gt;= a + c || c &gt;= a + b) &#123;</span><br><span class="line">            System.out.println(<span class="string">"三角形两边之和必须大于第三边"</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.a = a;</span><br><span class="line">        <span class="keyword">this</span>.b = b;</span><br><span class="line">        <span class="keyword">this</span>.c = c;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">calPerimeter</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 重写Shape类的计算周长的抽象方法</span></span><br><span class="line">        <span class="keyword">return</span> a + b + c;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getType</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 重写Shape类返回形状的抽象方法</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">"三角形"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面的<code>Triangle</code>类继承了<code>Shape</code>抽象类，并实现了<code>Shape</code>类中两个抽象方法，是一个普通类，因此可以创建<code>Triangle</code>类的实例，可以让一个<code>Shape</code>类型的引用变量指向<code>Triangle</code>对象。<br>下面再定义一个<code>Circle</code>普通类，也是<code>Shape</code>类的一个子类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Circle</span> <span class="keyword">extends</span> <span class="title">Shape</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> radius;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Circle</span><span class="params">(String color, <span class="keyword">double</span> radius)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(color);</span><br><span class="line">        <span class="keyword">this</span>.radius = radius;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setRadius</span><span class="params">(<span class="keyword">double</span> radius)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.radius = radius;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">double</span> <span class="title">calPerimeter</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 重写Shape类的计算周长的抽象方法</span></span><br><span class="line">        <span class="keyword">return</span> <span class="number">2</span> * Math.PI * radius;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getType</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 重写Shape类返回形状的抽象方法</span></span><br><span class="line">        <span class="keyword">return</span> getColor() + <span class="string">"圆形"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Shape s1 = <span class="keyword">new</span> Triangle(<span class="string">"黑色"</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line">        Shape s2 = <span class="keyword">new</span> Circle(<span class="string">"黄色"</span>, <span class="number">3</span>);</span><br><span class="line">        System.out.println(s1.getType());</span><br><span class="line">        System.out.println(s1.calPerimeter());</span><br><span class="line">        System.out.println(s2.getType());</span><br><span class="line">        System.out.println(s2.calPerimeter());</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面<code>main()</code>方法中定义了两个<code>Shape</code>类型的引用变量，分别指向<code>Triangle</code>和<code>Circle</code>。由于在<code>Shape</code>中定义了<code>calPerimeter()</code>方法和<code>getType()</code>方法，所以程序可以直接调用<code>s1</code>变量和<code>s2</code>变量的<code>calPerimeter()</code>方法和<code>getType()</code>方法，无需强制类型转换为其子类类型。<br>当使用<code>abstract</code>修饰类时，表明这个类只能被继承。用于修饰方法时，表明这个方法须由子类实现。</p>
<h2 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h2><p>接口定义的基本语法如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">[修饰符] <span class="class"><span class="keyword">interface</span> 接口名 <span class="keyword">extends</span> 父接口1， 父接口2...</span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    零到多个常量定义</span><br><span class="line">    零到多个抽象方法定义</span><br><span class="line">    零到多个内部类、接口、枚举定义</span><br><span class="line">    零到多个私有方法、默认方法或类方法定义</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>修饰符可以是<code>public</code>或者省略</li>
<li>一个接口可以有多个直接父接口，但接口只能继承接口，不能继承类<br>从某种程度上，接口可以被当成一个特殊的类，因此一个Java源文件里最多只能有一个<code>public</code>接口</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestInterface</span> </span>&#123;</span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//      Inter1 in = new Inter1(); //5、接口不能实例化</span></span><br><span class="line">                Inter1 in = <span class="keyword">new</span> Inter1Impl();<span class="comment">//多态</span></span><br><span class="line">                in.save();</span><br><span class="line">                in.update();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//1、定义接口,interface关键字</span></span><br><span class="line">        <span class="comment">//interface 接口名</span></span><br><span class="line">        <span class="class"><span class="keyword">interface</span> <span class="title">Inter1</span></span>&#123;</span><br><span class="line">            <span class="comment">//2、接口是一个特殊的抽象类，特殊在哪儿？-- 特殊在接口里都是抽象方法，没有普通方法</span></span><br><span class="line">            <span class="function"><span class="keyword">abstract</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">()</span> </span>;</span><br><span class="line">            <span class="function"><span class="keyword">abstract</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">()</span> </span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//3、通过implements，使用接口的功能</span></span><br><span class="line">        <span class="comment">//4、接口是一个特殊的抽象类，接口实现类的用法和继承时子类的用法一样？</span></span><br><span class="line">        <span class="comment">//实现类还是两条路---要么是抽象的实现类 -- 要么重写所有抽象方法</span></span><br><span class="line">        <span class="comment">//abstract class Inter1Impl  implements   Inter1&#123;</span></span><br><span class="line">        <span class="class"><span class="keyword">class</span> <span class="title">Inter1Impl</span>  <span class="keyword">implements</span>   <span class="title">Inter1</span></span>&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">"正在保存数据"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">"正在更新数据"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><ol>
<li>类和类的关系：继承 extends / 单继承 / 单根继承</li>
</ol>
<ul>
<li>1.1 继承的意义：为了提高代码的复用性，减少了代码的编写提高开发效率。</li>
<li>1.2 方法重写的意义：在不修改父类源码的前提下，在子类中重写业务，从此使用的就是重写后的功能。</li>
<li>1.3 要求子类的方法声明和父类一样，只要改方法体。</li>
<li>1.4 有了继承有了重写就产生了多态，多态的意义：为了统一程序的调用标准，标准就是父类。</li>
<li>多态 也就是向上转型/向下转型。</li>
<li>向下转型的意义：很少用，相当于想要使用子类的特殊功能，还不如直接创建子类对象简单。</li>
<li>class A extends B</li>
<li>其中，A和B都是类，A是子类，B是父类，A就拥有了B的所有功能(除了私有的和构造方法)</li>
<li>其他知识点：this 和super  ，构造方法，各种代码块…</li>
</ul>
</li>
<li><ol start="2">
<li>类和接口关系：实现implements  / 单实现 / 多实现<ul>
<li>class A implements B,C</li>
<li>其中，A是实现类,B和C是接口</li>
<li>要求A 可以把 B 和C 接口里的所有 抽象方法 都重写掉，否则 A 就是抽象类</li>
<li>接口不能创建对象</li>
<li>接口里没有构造方法，接口里都是常量，接口里都是抽象方法</li>
</ul>
</li>
</ol>
</li>
<li><ol start="3">
<li>接口和接口关系：继承extends / 单继承 / 多继承<ul>
<li>接口的多继承的关系，打破了java单继承的局限性</li>
<li>interface A  extends  B,C</li>
<li>其中，A B C 都是接口，A是子接口，同时拥有B和C接口里的所有功能</li>
<li>class AImpl implements  A</li>
<li>要求AImpl需要重写A接口里的所有方法(是包含B和C接口的所有方法)，否则就是抽象类</li>
</ul>
</li>
</ol>
</li>
<li><ol start="4">
<li>接口和抽象类的区别！！！</li>
</ol>
<ul>
<li>相同点：都是抽象层，都不能实例化</li>
<li>不同点：<ol>
<li>抽象类用abstract关键字描述，接口用interface</li>
<li>子类和抽象类之间是extends关系，实现类和接口之间是implements关系</li>
<li>抽象类中 可以  有构造方法 ，接口里 不能 出现 构造方法</li>
<li>抽象类里可以有 变量，接口里没有变量全都是静态的常量</li>
<li>接口里定义常量的语法：public static final String NAME=”jack”，会为变量自动拼接public static final</li>
<li>抽象类里 可以有普通方法  也可以有 抽象方法，接口都是抽象方法</li>
<li>抽象类和子类之间是继承关系，而且java中，只支持单继承</li>
<li>接口突破了java单继承的局限性，因为接口可以多继承也可以多实现，甚至可以继承的同时多实现</li>
<li>接口的复杂用法<ul>
<li>多继承： interface A  extends  B,C  其中A是子接口，同时拥有自己的和BC的功能</li>
<li>多实现： class AImpl implements M,N,O,P 其中AImpl是实现类，需要同时重写MNOP的所有抽象方法，否则就是一个抽象类</li>
<li>继承的同时多实现： class AImpl extends Object implements M,N 一定是先继承后实现</li>
</ul>
</li>
</ol>
</li>
</ul>
</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/04/20/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day8/" rel="prev" title="Java日拱一卒-day8">
      <i class="fa fa-chevron-left"></i> Java日拱一卒-day8
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/04/22/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day10/" rel="next" title="Java日拱一卒-day10">
      Java日拱一卒-day10 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#抽象方法"><span class="nav-number">1.</span> <span class="nav-text">抽象方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#接口"><span class="nav-number">2.</span> <span class="nav-text">接口</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
